package com.yumingjiang.weichatgtp.intercepter;

import com.yumingjiang.weichatgtp.common.exception.AuthException;
import com.yumingjiang.weichatgtp.service.JwtTokenService;
import com.yumingjiang.weichatgtp.service.impl.JwtTokenServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Administrator]
 * @version : [v1.0]
 * @createTime : [2023/3/12 3:04]
 */
public class WeiChatGPTAuthIntercepter implements HandlerInterceptor {
    private JwtTokenService jwtTokenService = new JwtTokenServiceImpl();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws AuthException {
        String accessToken = request.getHeader("accessToken");
        if (StringUtils.isBlank(accessToken)){
            throw new AuthException("403","accessToken缺失");
        }
        try{
            jwtTokenService.verifiedToken(accessToken);
        }catch (Exception e){
            throw new AuthException("403",e.getMessage());
        }

        return true;
    }
}
